﻿
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace QuizSilverlight
{
    /// <summary>
    /// Represents a person's contact information.
    /// </summary>
    public class Participant : INotifyPropertyChanged, IEditableObject
    {
        /// <summary>
        /// Represents the first name of the contact.
        /// </summary>
        private string firstName;

        /// <summary>
        /// Represents the last name of the contact.
        /// </summary>
        private string lastName;

        /// <summary>
        /// Represents the contact's phone number.
        /// </summary>
        private string phone;


        public Participant()
        {
        }

        /// <summary>
        /// Gets or sets the first name of the contact.
        /// </summary>
        [Required]
        [Display(Name = "First Name", Description = "Votre prénom", GroupName = "Name")]
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                if (value != firstName)
                {
                    firstName = value;
                    OnPropertyChanged("FirstName");
                }
            }
        }

        /// <summary>
        /// Gets or sets the last name of the contact.
        /// </summary>
        [Required]
        [Display(Name = "Last Name", Description = "Votre nom", GroupName = "Name")]
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                if (value != lastName)
                {
                    lastName = value;
                    OnPropertyChanged("LastName");
                }
            }
        }

        /// <summary>
        /// Gets or sets the phone number of the contact in the form (###) ###-####.
        /// </summary>
        [Display(Name = "Phone", Description = "Numéro de téléphone de la forme ## ### ###")]
        [RegularExpression(@"^\d\d\ \d\d\d\ \d\d\d$", ErrorMessage = "Le numéro de téléphone n'est pas valide.  Veuillez entrer un numéro de téléphone de la forme suivante ## ### ###")]
        public string Phone
        {
            get
            {
                return phone;
            }
            set
            {
                if (value != phone)
                {
                    Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Phone" });
                    phone = value;
                    OnPropertyChanged("Phone");
                }
            }
        }


        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Raised when a property on the contact changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// Gets a stock "John Doe" contact.
        /// </summary>
        public static Participant JohnDoe
        {
            get
            {
                return new Participant
                {
                    FirstName = "John",
                    LastName = "Doe",
                    Phone = "22 222 222",


                };
            }
        }



        #region IEditableObject Members
        /// <summary>
        /// Keeps a copy of the original contact for editing.
        /// </summary>
        private Participant cache;

        /// <summary>
        /// Indicates that the contact will undergo a cancellable edit.
        /// </summary>
        public void BeginEdit()
        {
            cache = new Participant();

            cache.FirstName = this.FirstName;
            cache.LastName = this.LastName;
            cache.Phone = this.Phone;

        }

        /// <summary>
        /// Indicates that the edit was cancelled and that the old state should be restored.
        /// </summary>
        public void CancelEdit()
        {

            this.FirstName = cache.FirstName;
            this.LastName = cache.LastName;
            this.Phone = cache.Phone;

            cache = null;
        }

        /// <summary>
        /// Indicates that the edit completed and that changed fields should be committed.
        /// </summary>
        public void EndEdit()
        {
            cache = null;
        }

        #endregion
    }
}